﻿<h2>Without bind or event</h2>
<p id="validate-text-initially-blank">
    Initially blank:
    <Validation AsyncValidator="@IsNotEmptyAsync">
        <TextEdit>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </TextEdit>
    </Validation>
</p>
<p id="validate-text-initially-populated">
    Initially populated:
    <Validation AsyncValidator="@IsNotEmptyAsync">
        <TextEdit Text="a">
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </TextEdit>
    </Validation>
</p>

<h2>With bind</h2>
<p id="validate-text-with-bind-initially-blank">
    Initially blank:
    <Validation AsyncValidator="@IsNotEmptyAsync">
        <TextEdit @bind-Text="@textWithBindInitiallyBlank">
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </TextEdit>
    </Validation>
</p>
<p id="validate-text-with-bind-initially-populated">
    Initially populated:
    <Validation AsyncValidator="@IsNotEmptyAsync">
        <TextEdit @bind-Text="@textWithBindInitiallyPopulated">
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </TextEdit>
    </Validation>
</p>

<h2>With event</h2>
<p id="validate-text-with-event-initially-blank">
    Initially blank:
    <Validation AsyncValidator="@IsNotEmptyAsync">
        <TextEdit Text="@textWithEventInitiallyBlank" TextChanged="@((v)=> { textWithEventInitiallyBlank = v; })">
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </TextEdit>
    </Validation>
</p>
<p id="validate-text-with-event-initially-populated">
    Initially populated:
    <Validation AsyncValidator="@IsNotEmptyAsync">
        <TextEdit Text="@textWithEventInitiallyPopulated" TextChanged="@((v)=> {textWithEventInitiallyPopulated = v; })">
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </TextEdit>
    </Validation>
</p>
@code{
    Random random = new();

    async Task IsNotEmptyAsync( ValidatorEventArgs e, System.Threading.CancellationToken cancellationToken )
    {
        cancellationToken.ThrowIfCancellationRequested();

        await Task.Delay( random.Next( 300 ) );

        e.Status = string.IsNullOrEmpty( Convert.ToString( e.Value ) )
            ? ValidationStatus.Error
            : ValidationStatus.Success;
    }

    string textWithBindInitiallyBlank;
    string textWithBindInitiallyPopulated = "a";

    string textWithEventInitiallyBlank;
    string textWithEventInitiallyPopulated = "a";
}